package top.gohigh.commonview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ContextThemeWrapper;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;

import androidx.appcompat.widget.AppCompatTextView;

/**
 * 倒计时+旋转 自定义背景
 */
public class GHCountDownTextView extends AppCompatTextView {

    ContextThemeWrapper  context;
    float fromDegrees=0;
    float toDegrees=360;
    float centerX;
    float centerY;
    float depthZ=1.0f;
    boolean reverse=true;
    int duration=1000;
    int countDownVal =3;

    Rotate3dAnimation rotate;

    public GHCountDownTextView(Context context) {
        super(context);

    }

    public GHCountDownTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public GHCountDownTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    /**
     * 布局完成后调用
     * @param fromDegrees
     * @param toDegrees

     */
    public void build(ContextThemeWrapper context, float fromDegrees, float toDegrees, int duration,int delay,boolean fillAfter,int countDownVal){

        this.context=context;
        centerX=this.getWidth()/2.0f;
        centerY=this.getHeight()/2.0f;
        this.duration=duration;
        this.fromDegrees=fromDegrees;
        this.toDegrees=toDegrees;
        rotate=new Rotate3dAnimation(context,fromDegrees,toDegrees,centerX,centerY,depthZ,reverse);
        rotate.setStartOffset(delay);
        rotate.setDuration(duration);
        rotate.setFillAfter(fillAfter);
        rotate.setInterpolator(new AccelerateInterpolator());
        this.countDownVal =countDownVal;
        rotate.setRepeatCount(countDownVal-1);
        this.setText(String.valueOf(countDownVal));

        rotate.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //隐藏
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                //计数减1
                GHCountDownTextView.this.countDownVal--;
                GHCountDownTextView.this.setText(String.valueOf( GHCountDownTextView.this.countDownVal));

            }
        });

    }

    public void start(){

        this.startAnimation(rotate);
    }


}
